#' reading in:
#'   us_secdef.csv (hand-coded table of secdef appointment dates and partisanship)
#' 
#' writing out: 
#'   us_secdef_table.tex (Table 2 in main text)
#'   

d = read_csv("source_data/us_secdef.csv")
d = d |> mutate(
  tenure = as.Date(end_date) - as.Date(start_date),
  tenure = tenure |> as.numeric()
)

summary_table = d |> group_by(pres_party) |>
  summarise(
    total_dem = sum(tenure[secdef_party=='D']) %>% `/`(365) |> round(0),
    total_gop = sum(tenure[secdef_party=='R']) %>% `/`(365) |> round(0),
    partisan_dem = sum(tenure[secdef_party=='D' & secdef_partisan==1]) %>% `/`(365) |> round(0),
    partisan_gop = sum(tenure[secdef_party=='R' & secdef_partisan==1]) %>% `/`(365) |> round(0),
    indep = sum(tenure[secdef_party=='I']) %>% `/`(365) |> round(0)
  )

summary_table |> xtable() |> print(file='tables/us_secdef_table.tex')


#' Note on calculating the total years of administration for each party (37 for Dem, 40 for GOP):
#' First, note that according to the data used here, total years of appointments under GOP presidents 
#'   sum up to 39 (instead of 40) due to vacancies
#' To calculate the totals: 
#' The office of Secretary of Defense was established September 17, 1947 (77 years ago).
#' Since that time, there have been ten complete Republican presidential terms 
#'   (Eisenhower x2, Nixon/Ford x2, Reagan x2, Bush Sr x1, Bush Jr x2, Trump x1), = 40 years. 
#' The remainder of the time has been Democratic presidency, 37 = 77-40.

